package com.healthcloud.service.impl;

import cn.jpush.api.bean.TestDateEntity;
import com.google.gson.Gson;
import com.healthcloud.entity.JpushInfoVo;
import com.healthcloud.entity.PpPatientBaseEntity;
import com.healthcloud.service.PpPatientBaseService;
import com.healthcloud.utils.IdcardInfoExtractorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.healthcloud.dao.JpushInfoDao;
import com.healthcloud.entity.JpushInfoEntity;
import com.healthcloud.service.JpushInfoService;
import com.healthcloud.utils.PageParam;
import org.springframework.transaction.annotation.Transactional;



@Service("jpushInfoService")
@Transactional
public class JpushInfoServiceImpl implements JpushInfoService {


	private static  final Logger LOG = LoggerFactory.getLogger(JpushInfoServiceImpl.class);

	@Autowired
	private JpushInfoDao jpushInfoDao;

	@Autowired
	private PpPatientBaseService ppPatientBaseService;
	
	@Override
	public JpushInfoEntity queryObject(Long id){
		return jpushInfoDao.queryObject(id);
	}
	
	@Override
	public List<JpushInfoEntity> queryList(JpushInfoEntity bean, PageParam pageParam){
		return jpushInfoDao.queryList(bean, pageParam);
	}
	
	@Override
	public int queryTotal(JpushInfoEntity bean){
		return jpushInfoDao.queryTotal(bean);
	}
	
	@Override
	public void save(JpushInfoEntity jpushInfo){
		jpushInfoDao.save(jpushInfo);
	}
	
	@Override
	public void update(JpushInfoEntity jpushInfo){
		jpushInfoDao.update(jpushInfo);
	}
	
	@Override
	public void delete(Long id){
		jpushInfoDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Long[] ids){
		jpushInfoDao.deleteBatch(ids);
	}


	@Override
	public void savePatient(TestDateEntity testDateEntity) {


		PpPatientBaseEntity ppPatientBase = ppPatientBaseService.queryByIdcard(testDateEntity.getPatientIdCard());

		if(ppPatientBase == null) {

			try {
				IdcardInfoExtractorUtil idcardInfoExtractorUtil = new IdcardInfoExtractorUtil(testDateEntity.getPatientIdCard());
				ppPatientBase = new PpPatientBaseEntity();
				ppPatientBase.setRealname(testDateEntity.getPatientName());
				ppPatientBase.setIdcard(testDateEntity.getPatientIdCard());
				ppPatientBase.setSourceType(2);
				ppPatientBase.setBirthday(idcardInfoExtractorUtil.getBirthday());
				ppPatientBase.setGender(Integer.valueOf(idcardInfoExtractorUtil.getGender()));
				ppPatientBase.setPhone(testDateEntity.getPatientPhone());
				ppPatientBase.setHeight(170.0);
				ppPatientBase.setUserimage(testDateEntity.getPatientImage());
				ppPatientBase.setRegisteraddress(testDateEntity.getPatientAddress());
				ppPatientBase.setCreatedtime(new Date());
				ppPatientBaseService.save(ppPatientBase);
			}catch (Exception e){
				LOG.error(e.getMessage());
			}
		}else{

			try {
				ppPatientBase.setUserimage(testDateEntity.getPatientImage());
				ppPatientBase.setRegisteraddress(testDateEntity.getPatientAddress());
				ppPatientBase.setUpdatedtime(new Date());
				ppPatientBaseService.update(ppPatientBase);
			}catch (Exception e){
				LOG.error(e.getMessage());
			}
		}
	}


	@Override
	public List<JpushInfoVo> getJpushInfo(String mKMedicalinstrumentId) {
		return jpushInfoDao.getJpushInfo(mKMedicalinstrumentId);
	}

	@Override
	public void saveJpushInfoEntity(TestDateEntity testDateEntity, String mKMedicalinstrumentId) {


		List<JpushInfoVo> list = jpushInfoDao.getJpushInfo(mKMedicalinstrumentId);
		try {
			for (JpushInfoVo jpushInfoVo : list) {
				JpushInfoEntity jpushInfo = new JpushInfoEntity();
				testDateEntity.setInstrumentName(jpushInfoVo.getInstrumentName());
				jpushInfo.setJpushInfo(new Gson().toJson(testDateEntity));
				jpushInfo.setUserId(jpushInfoVo.getUserId());
				jpushInfo.setIsRead(0);
				jpushInfo.setCreateTime(new Date());
				jpushInfoDao.save(jpushInfo);

			}
		}catch (Exception e){}


	}
}
